home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dialog / main.c < prev    next >
C/C++ Source or Header  |  1995-12-04  |  7KB  |  240 lines

  1. /*
  2.  *  dialog - Display simple dialog boxes from shell scripts
  3.  *
  4.  *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
  5.  *
  6.  *  This program is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU General Public License
  8.  *  as published by the Free Software Foundation; either version 2
  9.  *  of the License, or (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  *
  21.  *  HISTORY:
  22.  *
  23.  *  17/12/93 - Version 0.1 released.
  24.  *
  25.  *  19/12/93 - menu will now scroll if there are more items than can fit
  26.  *             on the screen.
  27.  *           - added 'checklist', a dialog box with a list of options that
  28.  *             can be turned on or off. A list of options that are on is 
  29.  *             returned on exit.
  30.  *
  31.  *  20/12/93 - Version 0.15 released.
  32.  *
  33.  *  29/12/93 - Incorporated patch from Patrick J. Volkerding
  34.  *             (volkerdi@mhd1.moorhead.msus.edu) that made these changes:
  35.  *             - increased MAX_LEN to 2048
  36.  *             - added 'infobox', equivalent to a message box without pausing
  37.  *             - added option '--clear' that will clear the screen
  38.  *             - Explicit line breaking when printing prompt text can be
  39.  *               invoked by real newline '\n' besides the string "\n"
  40.  *           - an optional parameter '--title <string>' can be used to
  41.  *             specify a title string for the dialog box
  42.  *
  43.  *  03/01/94 - added 'textbox', a dialog box for displaying text from a file.
  44.  *           - Version 0.2 released.
  45.  *
  46.  *  04/01/94 - some fixes and improvements for 'textbox':
  47.  *             - fixed a bug that will cause a segmentation violation when a
  48.  *               line is longer than MAX_LEN characters. Lines will now be
  49.  *               truncated if they are longer than MAX_LEN characters.
  50.  *             - removed wrefresh() from print_line(). This will increase
  51.  *               efficiency of print_page() which calls print_line().
  52.  *             - display current position in the form of percentage into file.
  53.  *           - Version 0.21 released.
  54.  *
  55.  *  05/01/94 - some changes for faster screen update.
  56.  *
  57.  *  07/01/94 - much more flexible color settings. Can use all 16 colors
  58.  *             (8 normal, 8 highlight) of the Linux console.
  59.  *
  60.  *  08/01/94 - added run-time configuration using configuration file.
  61.  *
  62.  *  09/01/94 - some minor bug fixes and cleanups for menubox, checklist and
  63.  *             textbox.
  64.  *
  65.  *  11/01/94 - added a man page.
  66.  *
  67.  *  13/01/94 - some changes for easier porting to other Unix systems (tested
  68.  *             on Ultrix, SunOS and HPUX)
  69.  *           - Version 0.3 released.
  70.  *  04/08/95 - Complete rewriten in C++. It is no more the official
  71.  *             dialog program. Not even sure its still work. The goal
  72.  *           was to turn all this into a library.
  73.  */
  74.  
  75.  
  76. #define VERSION    "C++ 0.3"
  77.  
  78. #include <stdlib.h>
  79. #include "dialog.h"
  80. #include "diadef.h"
  81. #ifdef HAVE_NCURSES
  82. #include "colors.h"
  83. #endif
  84.  
  85.  
  86. /*
  87.  * Print program usage
  88.  */
  89. static void Usage(char *name)
  90. {
  91.     fprintf(stderr,
  92.         "dialog version %s, by Savio Lam (lam836@cs.cuhk.hk).\n"
  93.         "\n"
  94.         "* Display dialog boxes from shell scripts *\n"
  95.         "\n"
  96.         "Usage: %s --clear\n"
  97.         "       %s --create-rc <file>\n"
  98.         "       %s [--title <title>] [--clear] <Box options>\n"
  99.         "\n"
  100.         "Box options:\n"
  101.         "\n"
  102.         "  --yesno     <text>\n"
  103.         "  --msgbox    <text>\n"
  104.         "  --infobox   <text>\n"
  105.         "  --inputbox  <text>\n"
  106.         "  --inputpass <text>\n"
  107.         "  --textbox   <file> <height> <width>\n"
  108.         "  --menu      <text> <menu height> <tag1> <item1>...\n"
  109.         "  --checklist <text> <list height> <tag1> <item1> <status1>...\n"
  110.         , VERSION, name, name, name);
  111.     exit (-1);
  112. }
  113.  
  114.  
  115.  
  116. int main(int argc, char *argv[])
  117. {
  118.     int offset = 0, clear_screen = 0, end_common_opts = 0;
  119.     int retval = 0;
  120.     char *title = NULL;
  121.  
  122.     if (argc < 2) {
  123.         Usage(argv[0]);
  124.     }else if (!strcmp(argv[1], "--create-rc")) {
  125.         #ifdef HAVE_NCURSES
  126.             if (argc != 3) {
  127.                 Usage(argv[0]);
  128.             }
  129.             create_rc(argv[2]);
  130.             return 0;
  131.         #else
  132.             fprintf(stderr, "\nThis option is currently unsupported on your system.\n");
  133.             return -1;
  134.         #endif
  135.     }
  136.  
  137.     while (offset < argc-1 && !end_common_opts) {    /* Common options */
  138.         if (!strcmp(argv[offset+1], "--title")) {
  139.             if (argc-offset < 3 || title != NULL) {    /* No two "--title" please! */
  140.                 Usage(argv[0]);
  141.             }else {
  142.                 title = argv[offset+2];
  143.                 offset += 2;
  144.             }
  145.         }else if (!strcmp(argv[offset+1], "--clear")) {
  146.             if (clear_screen) {    /* Hey, "--clear" can't appear twice! */
  147.                 Usage(argv[0]);
  148.             }else if (argc == 2) {    /* we only want to clear the screen */
  149.                 init_dialog();
  150.                 refresh();    /* init_dialog() will clear the screen for us */
  151.                 endwin();
  152.                 return 0;
  153.             }else {
  154.                 clear_screen = 1;
  155.                 offset++;
  156.             }
  157.         }else{    /* no more common options */
  158.             end_common_opts = 1;
  159.         }
  160.     }
  161.  
  162.     if (argc-1 == offset) {    /* no more options */
  163.         Usage(argv[0]);
  164.     }else if (!strcmp(argv[offset+1], "--yesno")) {
  165.         if (argc-offset != 5) {
  166.             Usage(argv[0]);
  167.         }
  168.         init_dialog();
  169.         retval = dialog_yesno(title, argv[offset+2],NULL)==MENU_YES
  170.             ? 0 : 1;
  171.     }else if (!strcmp(argv[offset+1], "--msgbox")) {
  172.         if (argc-offset != 5) {
  173.             Usage(argv[0]);
  174.         }
  175.         init_dialog();
  176.         retval = dialog_msgbox(title, argv[offset+2], 1);
  177.     }else if (!strcmp(argv[offset+1], "--infobox")) {
  178.         if (argc-offset != 5) {
  179.             Usage(argv[0]);
  180.         }
  181.         init_dialog();
  182.         retval = dialog_msgbox(title, argv[offset+2], 0);
  183.     }else if (!strcmp(argv[offset+1], "--textbox")) {
  184.         if (argc-offset != 5) {
  185.             Usage(argv[0]);
  186.         }
  187.         init_dialog();
  188.         retval = dialog_textbox(title, argv[offset+2], atoi(argv[offset+3])
  189.             ,atoi(argv[offset+4]));
  190.     }else if (!strcmp(argv[offset+1], "--menu")) {
  191.         int choice=0;
  192.         if (argc-offset < 5 || ((argc-offset-3) % 2)) {
  193.             Usage(argv[0]);
  194.         }
  195.         init_dialog();
  196.         if (dialog_menu(title, argv[offset+2], NULL,0
  197.             ,(argc-offset-3)/2, argv+offset + 3,choice)
  198.             == MENU_OK){
  199.             fprintf (stderr,"%s\n",argv[offset+3+choice*2]);
  200.         }
  201.     }else if (!strcmp(argv[offset+1], "--checklist")) {
  202.         if (argc-offset < 9 || ((argc-offset-6) % 3)) {
  203.             Usage(argv[0]);
  204.         }
  205.         init_dialog();
  206.         retval = dialog_checklist_p(title, argv[offset+2],NULL,
  207.                               atoi(argv[offset+3]),
  208.                               (argc-offset-6)/3, argv+offset + 6);
  209.     }else if (!strcmp(argv[offset+1], "--inputbox")) {
  210.         char inpstr[MAX_LEN+1];
  211.         if (argc-offset != 5) {
  212.             Usage(argv[0]);
  213.         }
  214.         init_dialog();
  215.         retval = dialog_inputbox(title, argv[offset+2], NULL, inpstr);
  216.         if (retval == 0) fprintf (stderr,"%s",inpstr);
  217.     }else if (!strcmp(argv[offset+1], "--inputpass")) {
  218.         char inpstr[MAX_LEN+1];
  219.         if (argc-offset != 5) {
  220.             Usage(argv[0]);
  221.         }
  222.         init_dialog();
  223.         retval = dialog_inputpass(title, argv[offset+2], NULL, inpstr);
  224.  
  225.         if (retval == 0) fprintf (stderr,"%s",inpstr);
  226.     }else{
  227.         Usage(argv[0]);
  228.     }
  229.     if (clear_screen) {    /* clear screen before exit */
  230.         attr_clear(stdscr, LINES, COLS, screen_attr);
  231.         refresh();
  232.     }
  233.     endwin();
  234.     return retval;
  235. }
  236.  
  237.  
  238.  
  239.  
  240.